package com.wcarmon.life.command{

    import com.adobe.cairngorm.commands.ICommand
    import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.IResponder;
	import com.wcarmon.life.model.LifeModelLocator;
	import com.wcarmon.life.event.SearchEvent;
	import com.wcarmon.life.business.*;
	import com.wcarmon.life.vo.BlockVO;
	import mx.utils.ObjectUtil;
	import mx.utils.StringUtil;
	import mx.events.CloseEvent;
	

	public class SearchCommand implements ICommand, IResponder {
		include "../../includes/loggerSnippet.as";
		
		public function execute( event : CairngormEvent ) : void {
			var e : SearchEvent = SearchEvent( event );
			var model : LifeModelLocator = LifeModelLocator.getInstance();
			model.searchIsInProgress = true;
			model.lastSearchQuery = e.query;
			log.debug('last search query: ' + e.query );
			var searchMode : String = ( model.ANDMode ) ? 'and' : 'or';
			log.debug('search mode ' + searchMode); 
			( new SearchDelegate(this) ).doSearch( e.query, searchMode );
		}
	
	
	
	
	 	public function result( event : Object ) : void {
	 		
			var e : ResultEvent = ResultEvent(event);
			var model : LifeModelLocator = LifeModelLocator.getInstance();

			model.lastSearchResults = e.result as Array;
			model.gotoScreen( LifeModelLocator.SCREEN_DASHBOARD );			
			model.searchIsInProgress = false;
		
			var Q : String = model.lastSearchQuery;
			var noResults : Boolean = ((e.result as Array).length == 0);
			var multipleTerms : Boolean = ( StringUtil.trim(Q).indexOf(" ") != -1 ) 
									|| ( Q.indexOf(",") != -1 );
			if( multipleTerms && noResults ){

				var modifiedQuery : String = replaceSpacesWithCommas(Q);
				var modifiedQueryIsDifferent : Boolean = (StringUtil.trim(modifiedQuery) != StringUtil.trim(Q) );

				if( modifiedQueryIsDifferent ){
					Alert.show('No Results.\nDo you want to try searching for \n"' + modifiedQuery + '"?' , 
							"No Search Results", 
							Alert.YES | Alert.NO, 
							null, 
							alertClickHandler);
				}
			}
		}
		public function fault( event : Object ) : void {
			LifeModelLocator.getInstance().searchIsInProgress = false;
			include "includes/FaultSnippet.as";
		}
	



		
		////////////////////////////////////////////////////////////////////////	
		private function replaceSpacesWithCommas( oldQuery : String ) : String{
			//replace commas with spaces (avoid double commas)
			var myPattern : RegExp = /\,/gi;
			var tmp : String = oldQuery.replace( myPattern, ' ' );
			
			//replace spaces with commas
			var myPattern2 : RegExp = / /gi;
			return tmp.replace(myPattern2, ', ');
		}
	
		private function alertClickHandler(event:CloseEvent) : void {
			var model : LifeModelLocator = LifeModelLocator.getInstance();
			var modifiedQuery : String = replaceSpacesWithCommas( model.lastSearchQuery );
			if( event.detail==Alert.YES ){
				log.debug("[SearchCommand.alertClickHandler] kicking off new search event for '"+ modifiedQuery +"'");
				( new SearchEvent(modifiedQuery) ).dispatch();
			}else{
				log.debug("[SearchCommand.alertClickHandler] you clicked no");
			}
		}	
	
	}//class
}//package